﻿var UserFunc = function(rHd){
    var __hd = (typeof(rHd)=="string")?rHd:"userFunc";
    var __data = null;
    var __target = "_blank";
    var __path = new Array();
    var __mode = "edit";
    var __buildList = function(rData){
        //parentId,id,menuName,url,order;
        var _str = new Array();
        _str.push('<div class="function_e_i" idx="'+i+'" id="'+rData.Id+'" parent="'+rData.ParentId+'">');
        if(rData.Id!="root"){//root
            if(rData.Children.length>0){
                _str.push('<div class="function_i_t">');
            }else{
                _str.push('<div class="function_i_l">');
            }
            _str.push('<input type="checkbox" name="cbx_function" onclick="'+__hd+'.onItemCheck(this,event);" id="cbx_'+rData.Id+'" value="'+rData.Id+'" />');
            _str.push('<span onclick="'+__hd+'.onItemClick(this,event)">');
            _str.push(rData.Name);
            if(rData.Children.length<=0 && rData.MenuName.trim()!=""){
                _str.push('<font class="function_menu_flg">(M)</font>');
            }
            _str.push('</span>');
            //_str.push('('+rData.MenuName+')');
            _str.push('</div>');
            _str.push('<div class="function_e_sub">');
        }else{
            _str.push('<div>');
        }
        for(var i=0;i<rData.Children.length;i++){
            _str.push(__buildList(rData.Children[i]));
        }
        _str.push('</div>');
        _str.push('</div>');
        return _str.join('');
    };
    var __buildGridView = function(rData){
        var _str = new Array();
        _str.push('<table class="TreeGridView" cellspacing="0">');
        _str.push('<tr class="TreeGridViewHeader">');
        _str.push('<th>功能名称</th>');
        _str.push('<th>菜单名称</th>');
        _str.push('<th>Url</th>');
        _str.push('<th>Pattern</th>');
        _str.push('<th>排序</th>');
        _str.push('<th width="30px">开放</th>');
        _str.push('<th>GUID</th>');
        _str.push('</tr>');
        for(var i=0;i<rData.Children.length;i++){
            _str.push(__buildGridViewRow(rData.Children[i],0));
        }
        _str.push('</table>');
        return _str.join('');
        
    };
    var __buildGridViewRow = function(rData,rLayer){
        var _str = new Array();
        if(rLayer==0){
            _str.push('<tr class="TreeGridViewRowf">');
        }else{
            _str.push('<tr class="TreeGridViewRow">');
        }
        _str.push('<td><div style="padding-left:'+(rLayer*20)+'px">');
        if(rData.Name!="/"){//root
            _str.push('<input type="checkbox" name="cbx_function" id="cbx_'+rData.Id+'" value="'+rData.Id+'" />');
        }
        _str.push('<a href="Admin/Permission/Func/Edit.aspx?id='+rData.Id+'">'+rData.Name+'</a></div></td>');
        _str.push('<td>'+rData.MenuName+'</td>');
        _str.push('<td>'+rData.Url+'</td>');
        _str.push('<td>'+rData.Pattern+'</td>');
        _str.push('<td>'+rData.Order+'</td>');
        _str.push('<td><div class="sign_yn'+rData.FlgOrgOpen+'"></div></td>');
        _str.push('<td>'+rData.Id+'</td>');
        _str.push('</tr>');
        rLayer++;
        for(var i=0;i<rData.Children.length;i++){
            _str.push(__buildGridViewRow(rData.Children[i],rLayer));
        }
        return _str.join('');
    };
    var __menuLinkIdx = 0;
    var __buildMenu = function(rData){
        //parentId,id,menuName,url,order;
        var _str = new Array();
        var _url = window.location.href;
        if(rData.Id!="root"){//root
            _str.push('<div class="function_i" idx="'+i+'" id="'+rData.Id+'" parent="'+rData.Id+'">');
            
            if(rData.Children!=null && rData.Children.length>0){
                _str.push('<div class="function_i_t">');
            }else{
                _str.push('<div class="function_i_l">');
            }
            if(rData.Url!=""){//节点
                var reg = new RegExp(rData.Pattern,"gi");
                var _linkCss = (reg.test(_url))?"function_l_c":"function_l";
                reg = null;
                _str.push('<a href="'+rData.Url+'" class="'+_linkCss+'" id="menu_link_0'+__menuLinkIdx+'" target="'+__target+'">'+rData.MenuName+'</a>');
                __menuLinkIdx++;
            }else{
                _str.push(rData.MenuName);
            }
            _str.push('</div>');
            if(rData.Children!=null && rData.Children.length>0){
                _str.push('<div class="function_sub">');
                for(var i=0;i<rData.Children.length;i++){
                    _str.push(__buildMenu(rData.Children[i]));
                }
                _str.push('</div>');
            }
            _str.push('</div>');
        }else{
            for(var i=0;i<rData.Children.length;i++){
                _str.push(__buildMenu(rData.Children[i]));
            }
        }
        return _str.join('');
    };
    var __bindRole = function(rRoleFuncList){
        var cbx = null;
        for(var i=0;i<rRoleFuncList.length;i++){
            cbx = $("#cbx_"+rRoleFuncList[i]);
            if(cbx!=null){cbx.checked = true;}
        }
    };
    var __selectedItem = null;
    var __loadData = new Array();
    var __checkSub = function(rBox,rChecked){
        if(rBox==null){return;}
        if(rBox.tagName=="INPUT" && rBox.getAttribute("type")=="checkbox"){
            rBox.checked = rChecked;
        }
        if(rBox.childNodes!=null && rBox.childNodes.length>0){
            for(var i=0;i<rBox.childNodes.length;i++){
                __checkSub(rBox.childNodes[i],rChecked);
            }
        }
    };

    this.bindRole = function(rRoleFuncList){
        __bindRole(rRoleFuncList);
    };
    this.onItemCheck = function(sender,e){
        var subContainer = sender.parentNode.parentNode.childNodes[1];
        __checkSub(subContainer,sender.checked);
        var _node = sender.parentNode.parentNode;
        while(_node.tagName=="DIV" && _node.getAttribute("parent")!=""){
            _node = _node.parentNode.parentNode;
            _node.childNodes[0].childNodes[0].checked = true;
        }
        
    };
    this.onItemClick = function(sender,e){
        var item = sender.parentNode.parentNode;
        var subObj = item.childNodes[1];
        var idx = parseInt(item.getAttribute("idx"));
        var key = item.getAttribute("id");
        if(subObj.style.display=="none"){
            subObj.style.display = "block";
        }else{
            subObj.style.display = "none";
        }
        item = subObj = idx = key = null;
    };
    this.setTarget = function(v){
        __target = v;
    };
    this.displayGridView = function(rData,rMode){
        __data = rData;
        if(typeof(rMode)=="string"){
            __mode = rMode;
        }
        var _str = __buildGridView(__data);
        document.write(_str);
    };
    this.display = function(rData,rMode){
        __data = rData;
        if(typeof(rMode)=="string"){
            __mode = rMode;
        }
        var _str = __buildList(__data);
        document.write(_str);
    };
    this.displayMenu = function(rData){
        __data = rData;
        var _str = '<div id="function_menu_container" class="function_container">';
        //_str += '<div class="function_shadow"></div>';
        _str += '<div class="function_body">'+__buildMenu(__data)+'</div>';
        _str += '</div>';
        document.write(_str);
        _str = null;
    };
    
};
